package com.lx.controller;

import com.lx.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AnnotationTest {

    //返回字符串
    @RequestMapping("/h1")
    public String getHello1(Model model) {
        model.addAttribute("msg", "hello,SpringMVCAnnotation!");
        return "hello";
    }

    //返回ModelAndView
    @RequestMapping("h2")
    public ModelAndView getHello2() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "hello2");
        modelAndView.setViewName("hello");
        return modelAndView;
    }


    //返回基本类型
    @ResponseBody
    @RequestMapping("h3")
    public String getHello3() {
        return "hello,h3";
    }

    //返回对象
    @ResponseBody
    @RequestMapping("h4")
    public User getHello4() {
        User user = new User();
        user.setAge(11);
        user.setName("张三");
        return user;
    }

    @ResponseBody
    @RequestMapping("h5")
    public List<Object> getHello5() {
        List list = new ArrayList();
        list.add("hello");
        list.add("h3");
        User user = new User();
        user.setAge(11);
        user.setName("张三");
        list.add(user);
        return list;
    }

    @ResponseBody
    @RequestMapping("h6")
    public Map<String, Object> getHello6() {
        Map map = new HashMap();
        map.put("o1", 111);
        map.put("o2", "oooo2");
        User user = new User();
        user.setAge(11);
        user.setName("张三");
        map.put("o3", user);
        return map;
    }
}
